package com.suwish.pc.ui.component;

import static com.suwish.pc.util.ResourceUtils._Icon;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;

import javax.swing.JPanel;

import com.suwish.pc.Setting;
/**
 * 
 * @author Min
 *
 */
public class UIImagePanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Image image = null;
	private int width = 0;
	private int height = 0;
	private Image defImage = null;
	
	public UIImagePanel(){
		this(null, 0, 0);
	}
	public UIImagePanel(Image image, int width, int height){
		this.image = image;
		this.width = width;
		this.height = height;
		defImage = _Icon(Setting.get("device.screen.bg")).getImage();
		setOpaque(false);
		refresh(image, width, height);
	}
	public void refresh(Image image){
		refresh(image, 320, 500);
	}
	public void refresh(Image image, int width, int height){
		if(image == null) return;
		this.image = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
		invalidate();
        validate();
        repaint();
	}
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D)g;
		if(image != null){
			g2d.drawImage(image, 0, 0, this);
		}else{			
			g2d.drawImage(defImage, 0, 0, this);
		}
	}
	
	@Override
	public Dimension getPreferredSize() {
		return new Dimension(320, 500);// new Dimension(width == 0 ? 320 : width, height == 0 ? 500 : height);
	}
}
